package com.dadi.aidlclient;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.dadi.testaidl.IMyAidlInterface;
import com.dadi.testaidl.Person;

public class MainActivity extends AppCompatActivity {
    private EditText etA, etB;
    private TextView tvResult;
    private IMyAidlInterface mService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("liaoming", "onCreate");
        setContentView(R.layout.activity_main);
        initViews();
        try {
            bindService();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void bindService() {
        Intent intent = new Intent();
        intent.setAction("com.dadi.testaidl.MAidlService");
        intent.setPackage("com.dadi.testaidl");
        startService(intent);
        bindService(intent, mServiceC,BIND_AUTO_CREATE);


    }

    private void initViews() {
        etA = findViewById(R.id.et_num1);
        etB = findViewById(R.id.et_num2);
        tvResult = findViewById(R.id.tv);
    }

    public void add(View view) {
        int a1 = Integer.valueOf(etA.getText().toString());
        int b1 = Integer.valueOf(etB.getText().toString());
        int result = 0;
        try {
            result = mService.add(a1, b1);
            Person person1 = new Person();
            person1.setName("刘大");
            person1.setAge(3);
            Person person2 = new Person();
            person2.setName("赵二");
            person2.setAge(4);
            Person person3 = new Person();
            person3.setName("张三");
            person3.setAge(5);
            tvResult.setText(result + "\n"+
                    mService.inPerson(person1) + "\n" +
                    mService.outPerson(person2) + "\n" +
                    mService.inOutPerson(person3) + "\n" +
                    person1.toString() + "\n" +
                    person2.toString() + "\n" +
                    person3.toString());
            Log.i("liaoming,", "result : " + result);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
    ServiceConnection mServiceC = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i("liaoming", "onServiceConnected");
            mService = IMyAidlInterface.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i("liaoming", "onServiceDisconnected");
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mServiceC);
        Log.i("liaoming", "onDestroy");
    }
}
